# 备忘录模式

# ⭐=> 保存一个对象的某个状态, 以便在适当的时候恢复对象

# 角色
# 备忘录: 存储对象的状态
# 发起人: 创建备忘录, 并在需要的时候恢复备忘录
# 负责人: 保存备忘录, 并在需要的时候提供备忘录

class Memento:
    def __init__(self, state):
        self.state = state

class Originator:
    def __init__(self, state):
        self.state = state
    def create_memento(self):
        return Memento(self.state)
    def restore_memento(self, memento):
        self.state = memento.state
    def show_state(self):
        print("State: " + self.state)

class Caretaker:
    def __init__(self):
        self.mementos = []
    def add_memento(self, memento):
        self.mementos.append(memento)
    def get_memento(self, index):
        return self.mementos[index]

if __name__ == "__main__":
    originator = Originator("State 1")
    originator.show_state()
    caretaker = Caretaker()
    caretaker.add_memento(originator.create_memento())
    originator.state = "State 2"
    originator.show_state()
    originator.restore_memento(caretaker.get_memento(0))
    originator.show_state()
    # Output: State: State 1 State: State 2 State: State 1